package com.yjhb.android.sbz.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.view.CircleImageView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2020/11/19 17:16
 */
public class TopRecomAdapter extends BaseRecyclerAdapter<TopRecomAdapter.TopHolder, TaskInfo> {
    private final String TAG = "TopRecomAdapter";


    public TopRecomAdapter(Context context) {
        super(context);
    }

    @Override
    protected TopHolder getHolder(View v) {
        return new TopHolder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_task_top;
    }

    @Override
    public void onBindViewHolder(@NonNull TopHolder topHolder, int i) {
        try {
            if (data == null || data.size() - 1 < i) {
                topHolder.itemView.setVisibility(View.GONE);
            } else {
                topHolder.itemView.setVisibility(View.VISIBLE);
                Glide.with(context).load(entity.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(topHolder.headIv);
                topHolder.titleTv.setText(entity.getTitle());
                BigDecimal bigDecimal = Tool.getPrettyNumber(entity.getMoney());
                CharSequence money = "赏金" + bigDecimal + "元";
                money = ZxSpanTool.ZoomSpan(money, 2, 2 + bigDecimal.toString().length(), 1.5f);
                topHolder.moneyTv.setText(money);
                topHolder.successTv.setText(entity.getSuccess() + "人");
                topHolder.remainderTv.setText(entity.getCurrentStock() + "个");
                topHolder.label1.setText(Tool.idTransformString(entity.getCategoryId()));
                topHolder.label2.setText(entity.getName());

                if (entity.getCreditMoney().intValue() > 0) {
                    topHolder.bao_tv.setVisibility(View.VISIBLE);
                } else {
                    topHolder.bao_tv.setVisibility(View.GONE);
                }
                if (entity.isRecommend()) {
                    topHolder.ding_tv.setVisibility(View.VISIBLE);
                } else {
                    topHolder.ding_tv.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {

        }
    }

//    @Override
//    public int getItemCount() {
//        return 4;
//    }
    @Override
    public int getItemCount() {
        if (data == null) return 0;
        if (data.size() > 4) return 4;
        return data.size();
    }

    class TopHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.head_iv)
        CircleImageView headIv;
        @BindView(R.id.title_tv)
        TextView titleTv;
        @BindView(R.id.money_tv)
        TextView moneyTv;
        @BindView(R.id.success_tv)
        TextView successTv;
        @BindView(R.id.remainder_tv)
        TextView remainderTv;
        @BindView(R.id.info_layout)
        LinearLayout infoLayout;
        @BindView(R.id.label1)
        TextView label1;
        @BindView(R.id.label2)
        TextView label2;
        @BindView(R.id.ding_tv)
        TextView ding_tv;
        @BindView(R.id.bao_tv)
        TextView bao_tv;
        @BindView(R.id.label_layout)
        LinearLayout labelLayout;

        public TopHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
